﻿using UnityEngine;
using System;
namespace CommonLib.Pattern.Pool {

	public class ReusableObject : MonoBehaviour, IReusable
	{
		public GameObject Child { get; set; }
		public Action<ReusableObject> RecycleAction { get; set; }
		public Action<ReusableObject> ResumeAction { get; set; }
		protected bool IsCreated { get; set; } = false;


		virtual public void Recycle()
		{
			if(null == Child)
			{
				//TODO 
				Debug.LogError("可重用资源丢失");
				return;
			}
			if (Child.transform.parent != this.transform)
				Child.transform.SetParent(this.transform);
			Child.SetActive(false);
			RecycleAction?.Invoke(this);
		}

		virtual public void Resume()
		{
			//throw new System.NotImplementedException();
			if (!IsCreated)
			{
				Child = CreateSelf();
				Child.transform.SetParent(transform);
			}
			Child.SetActive(true);
			ResumeAction?.Invoke(this);
		}

	  	virtual public void UnUse()
		{
			ObjectPool.Instance.RecycleObject(this);
		}

		virtual protected GameObject CreateSelf()
		{
			IsCreated = true;
			return null;
		}


		//将自身暴露在外
		//public static ReusableObject Craete(string typeName)
		//{
		//	GameObject prefab =
		//		GameResourceLoader.Instance.LoadFromResource<GameObject>(GameResource.ReusableResMgr.GetPath(AppPath.Resource, typeName));
		//	return GameObject.Instantiate(prefab).GetComponent<ReusableObject>();
		//}
	}
}